package com.jec.module.sysmonitor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jec.module.sysconfig.dao.TerminalBusinessViewRepository;
import com.jec.module.sysconfig.dao.TerminalKeyConfigViewRepository;
import com.jec.module.sysconfig.entity.vo.TerminalBusinessView;
import com.jec.module.sysconfig.entity.vo.TerminalKeyConfigView;
import com.jec.module.sysmonitor.dao.DevicePortRepository;
import com.jec.module.sysmonitor.dao.NetUnitRepository;
import com.jec.module.sysmonitor.dao.TerminalDeviceRepository;
import com.jec.module.sysmonitor.dao.TerminalDeviceViewRepository;
import com.jec.module.sysmonitor.entity.DevicePort;
import com.jec.module.sysmonitor.entity.TerminalDevice;
import com.jec.module.sysmonitor.entity.view.TerminalDeviceView;

/**
 * Created by jeremyliu on 6/3/16.
 */
@Service
public class NetworkDeviceService {

	@Autowired
    private TerminalDeviceViewRepository terminalDeviceViewRepository;

	@Autowired
    private TerminalDeviceRepository terminalDeviceRepository;

    @Autowired
    private NetUnitRepository netUnitRepository;
    
    @Autowired
    private DevicePortRepository devicePortRepository;

    @Autowired
    private TerminalBusinessViewRepository terminalBusinessViewRepository;

    @Autowired
    private TerminalKeyConfigViewRepository terminalKeyConfigViewRepository;
    
    //@Autowired
    //private SysLogService sysLogService;

    @Transactional(readOnly = true)
    public List<TerminalDeviceView> getAllDevice(int netunit){
        if(netunit>0)
            return terminalDeviceViewRepository.findAllByNetUnitId(netunit);
        else
            return terminalDeviceViewRepository.findAll();
    }

    //@Transactional
    public int createDevice(int netUnitId,String name, String code, String location, String category){
        if(netUnitRepository.findOne(netUnitId) == null)
            return -1;
        if(terminalDeviceRepository.findAllByCode(code).size()>0)
            return -2;

        TerminalDevice terminalDevice = new TerminalDevice();
        terminalDevice.setName(name);
        terminalDevice.setNetUnitId(netUnitId);
        terminalDevice.setCode(code);
        terminalDevice.setLocation(location);
        terminalDevice.setCategory(category);
        terminalDevice = terminalDeviceRepository.save(terminalDevice);
        
        //sysLogService.addLog("添加设备：" + name);
        
        return terminalDevice.getId();
    }

    //@Transactional
    public int modifyDevice(int id, Integer netUnitId, String name, String code, String location, String category){
        TerminalDevice terminalDevice = terminalDeviceRepository.findOne(id);
        if(terminalDevice == null)
            return 0;

        if(netUnitRepository.findOne(netUnitId) == null)
            return -1;
        if(terminalDeviceRepository.findAllByCodeAndIdNot(code, id).size()>0)
            return -2;
        if(name != null)
            terminalDevice.setName(name);
        if(netUnitId != null)
            terminalDevice.setNetUnitId(netUnitId);
        if(code!=null)
            terminalDevice.setCode(code);
        if(category != null)
        	terminalDevice.setCategory(category);
        terminalDevice.setLocation(location);
        terminalDeviceRepository.save(terminalDevice);
        
        //sysLogService.addLog("修改设备：" + name);
        
        return id;
    }

    //@Transactional
    public boolean removeDevice(int id){
    	
    	TerminalDevice device = terminalDeviceRepository.findOne(id);
    	if(device == null) {
    		return true;
    	}

        try {
            List<DevicePort> ports = devicePortRepository.findAllByDeviceId(id);
            devicePortRepository.delete(ports);
            terminalDeviceRepository.delete(id);
            
            //sysLogService.addLog("删除设备：" + device.getName());
            
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Transactional(readOnly = true)
    public List<DevicePort> getDevicePort(int deviceId){
        return devicePortRepository.findAllByDeviceId(deviceId);
    }

    @Transactional
    public int createDevicePort(int deviceId, String number, String function, boolean enable){
        if(terminalDeviceRepository.findOne(deviceId) == null)
            return -1;
        
        if(devicePortRepository.existsByDeviceIdAndNumberAndIdNot(deviceId, number, 0)) {
        	return -2;
        }
        
        DevicePort devicePort = new DevicePort();
        devicePort.setNumber(number);
        devicePort.setDeviceId(deviceId);
        /*
        if(devicePortDao.exist(devicePort))
            return -2;
            */
        devicePort.setFunction(function);
        devicePort.setEnable(enable);
        devicePort = devicePortRepository.save(devicePort);
        return devicePort.getId();
        /*
        if(devicePortDao.save(devicePort))
             return devicePort.getId();
        else
            return -1;
            */
    }

    @Transactional
    public boolean modifyDevicePort(int oldId, String number, String function,boolean enable){
        DevicePort devicePort = devicePortRepository.findOne(oldId);
        if(devicePort == null || number == null)
            return false;
        if(number != null && 
        		devicePortRepository.existsByDeviceIdAndNumberAndIdNot(
        				devicePort.getDeviceId(),
        				devicePort.getNumber(),
        				devicePort.getId()))
            return false;
        
        devicePort.setNumber(number);
        devicePort.setFunction(function);
        devicePort.setEnable(enable);
        
        devicePortRepository.save(devicePort);
        return true;
        
        //return devicePortDao.updatePort(oldId,number,function, enable) > 0;
    }

    @Transactional
    public boolean removeDevicePort(int id){
    	devicePortRepository.delete(id);
    	return true;
        //return devicePortDao.removeById(id);
    }

    @Transactional(readOnly = true)
    public List<TerminalBusinessView> getTerminalBusiness(String number){
        return terminalBusinessViewRepository.findAllByDeviceNumber(number);
    }

    @Transactional(readOnly = true)
    public List<TerminalKeyConfigView> getTerminalKeyConfig(String number){
        return terminalKeyConfigViewRepository.findAllByDeviceNumber(number);
    }
}
